require "Common/define"
require "Logic/Config"
require "Common/functions"

UIBottom = {};
HelperFunc.SetUIEnv(UIBottom)
local this = UIBottom;

this.LuaBehaviour = nil;
this.transform = nil;
this.gameObject = nil;
this.scaleEffectFinish = false

this.bt_home = nil;
this.bt_town = nil;
this.bt_adventure = nil;
this.bt_battle = nil;
this.bt_player = nil;
this.bt_hero = nil;
this.lastSelectedIndex = nil;
this.silenceCheckTime = -600;

--回调

this.hasInit = false;
this.pageController = nil;
this.page = nil

-- 首次打开主城
this.firstOpenCity = false

function UIBottom.Init(ignoreEnterBattle, page)
    this.ignoreEnterBattle = ignoreEnterBattle;
    local name, package = getUIName(PanelNames.UIBottom);
    panelMgr:ShowUI(name, package, this.onCreate, Config.useLocalUI);
    this.page = page
end

--定义控件
function UIBottom.onCreate(obj)
    this.firstOpenCity = false
    
    if not hasInit then
        hasInit = true;
        gameObject = obj;
        --transform = obj.transform;
        --LuaBehaviour = transform:GetComponent('LuaBehaviour');
        --transform:SetAsFirstSibling();

        --bottom
        --bt_home = obj:GetChild("HomeBtn");
        pageController = obj:GetController("page");
        bt_town = obj:GetChild("TownBtn");
        bt_town.gameObjectName = "TownBtn";
        bt_adventure = obj:GetChild("AdventureBtn");
        bt_adventure.gameObjectName = "AdventureBtn";
        bt_battle = obj:GetChild("BattleBtn");
        bt_battle.gameObjectName = "BattleBtn";
        bt_player = obj:GetChild("PlayerBtn");
        bt_player.gameObjectName = "PlayerBtn";
        bt_hero = obj:GetChild("HeroBtn");
        bt_hero.gameObjectName = "HeroBtn";
        this.adventureRedDotsName = gameObject.gameObjectName .. bt_adventure.gameObjectName;
        this.townRedDotsName = gameObject.gameObjectName .. bt_town.gameObjectName;
        this.heroRedDotsName = gameObject.gameObjectName .. bt_hero.gameObjectName;
        this.playerRedDotsName = gameObject.gameObjectName .. bt_player.gameObjectName;
        this.battleRedDotsName = gameObject.gameObjectName .. bt_battle.gameObjectName;
        this.addEvent();
        lastSelectedIndex = 0;
        this.UpdateCheck()

        -- 如果主城是立绘模式，则优先进入主城
        local cityMode = tonumber(GetPlayerPrefs("city_mode") or 1)
        if cityMode == 1 and logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.City, false) and not logicMgr.GuideMgr.HasGuidingInStage(logicMgr.MapManager.getMapId()) then
            this.firstOpenCity = true
            UIBottom.onSetTown()
        end
    end
    if not this.ignoreEnterBattle and not this.firstOpenCity then
        this.onClickBattle();
    end

    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBottom, Config.FuncOpenType.City, bt_town.name, bt_town);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBottom, Config.FuncOpenType.Adventure, bt_adventure.name, bt_adventure);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBottom, Config.FuncOpenType.Battle, bt_battle.name, bt_battle);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBottom, Config.FuncOpenType.Draw, bt_hero.name, bt_hero);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBottom, Config.FuncOpenType.Hero, bt_battle.name, bt_battle);
    --更新天灾入侵红点
    if logicMgr.InvasionManager.IsOpen() then
        InvasionHandler.ReqInitTeamBoss(0)
    end
    --this.updateTimer=utimer.timeCall(function()
    --    if FuncOpenManager.isOpen(Config.FuncOpenType.InvasionBattle,false) then
    --        local open=logicMgr.InvasionManager.SetInvasionFirstOpen()
    --        if open then
    --            if this.updateTimer~=nil then
    --                utimer.remove(this.updateTimer)
    --                this.updateTimer=nil
    --            end
    --        end
    --    end
    --end,5000,-1)
    if this.page == 3 then
        UIBottom.onClickPlayer()
        pageController.selectedIndex = 3
    end
end

function UIBottom.OnDestroy()
    hasInit = false;
    this.removeEvent();
    if this.updateTimer ~= nil then
        utimer.remove(this.updateTimer)
        this.updateTimer = nil
    end
end

function UIBottom.refreshRedDos()

    UIRedDots.addViewListen(PanelNames.UIBottom, this.townRedDotsName, bt_town, Config.RedDotsType.RootCity);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.heroRedDotsName, bt_hero, Config.RedDotsType.Summon, nil, {1, 10});
    UIRedDots.addViewListen(PanelNames.UIBottom, this.heroRedDotsName, bt_hero, Config.RedDotsType.DrawPond, {1, 10});
    UIRedDots.addViewListen(PanelNames.UIBottom, this.playerRedDotsName, bt_player, Config.RedDotsType.Hero);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.battleRedDotsName, bt_battle, Config.RedDotsType.RootBattle);
end

function UIBottom.addEvent()

    --bt_home.onClick:Add(this.onClickHome);
    bt_town.onClick:Add(this.onClickTown);
    bt_adventure.onClick:Add(this.onClickAdventure);
    bt_battle.onClick:Add(this.onClickBattle);
    bt_player.onClick:Add(this.onClickPlayer);
    bt_hero.onClick:Add(this.onClickHero);
    ListenEvent(Config.EventType.Fresh_Draw, this.refreshRedDos);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.townRedDotsName, bt_town, Config.RedDotsType.RootCity);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.heroRedDotsName, bt_hero, Config.RedDotsType.Summon, nil, {1, 10});
    UIRedDots.addViewListen(PanelNames.UIBottom, this.heroRedDotsName, bt_hero, Config.RedDotsType.DrawPond, {1, 10});
    UIRedDots.addViewListen(PanelNames.UIBottom, this.playerRedDotsName, bt_player, Config.RedDotsType.Hero);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.playerRedDotsName, bt_player, Config.RedDotsType.RootHero);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.battleRedDotsName, bt_battle, Config.RedDotsType.RootBattle);
    UIRedDots.addViewListen(PanelNames.UIBottom, this.adventureRedDotsName, bt_adventure, Config.RedDotsType.Adventure);

    --UIRedDots.addViewListen(PanelNames.UIBottom,gameObject.name..bt_hero.name,bt_hero,Config.RedDotsType.Summon);
end

function UIBottom.removeEvent()

    --bt_home.onClick:Remove(this.onClickHome);
    bt_town.onClick:Remove(this.onClickTown);
    bt_adventure.onClick:Remove(this.onClickAdventure);
    bt_battle.onClick:Remove(this.onClickBattle);
    bt_player.onClick:Remove(this.onClickPlayer);
    bt_hero.onClick:Remove(this.onClickHero);
    UIRedDots.removeViewEvent(PanelNames.UIBottom)

    CloseEvent(Config.EventType.Fresh_Draw, this.refreshRedDos)

    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIBottom);

end

function UIBottom.OnEnable()
end

function UIBottom.OnDisable()
end

--function UIBottom.onClickHome(isOn)
--    if pageController.selectIndex == 0 then
--        this.closeAllUI();
--        if not isOpenUI(PanelNames.UIHome) then
--            showUI(PanelNames.UIHome);
--        end
--        if not isOpenUI(PanelResNames.UIHeadMenu) then
--            showUI(PanelNames.UIHeadMenu)
--        end
--    end
--end

function UIBottom.onClickTown(isOn)
    if lastSelectedIndex == 0 then
        return ;
    end
    this.DoClickTown()
end
function UIBottom.DoClickTown()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.City, true, bt_town) then
        logicMgr.LBSManager.AutoJoinRoom()
        lastSelectedIndex = 0;
        pageController.selectedIndex = lastSelectedIndex;
        if isOpenUI(PanelResNames.UICity) then
            return ;
        end
        this.closeAllUI();
        if not isOpenUI(PanelResNames.UICity) then
            showUI(PanelNames.UICity);

            if not isOpenUI(PanelNames.UIFuncMenu) then
                showUI(PanelNames.UIFuncMenu);
            else
                getUI(PanelNames.UIFuncMenu):GetController("c1").selectedIndex = 0;
            end
        end
        if not isOpenUI(PanelResNames.UIHeadMenu) then
            showUI(PanelNames.UIHeadMenu);
        end
        if not isOpenUI(PanelNames.BackGround) then
            showUI(PanelNames.BackGround);
        end
        UIHeadMenu.SetNoticeDown(false)
        FireEvent(Config.EventType.BottomChangePage, EnumConst.BottomPageType.city)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
end
function UIBottom.onClickAdventure(isOn)
    if lastSelectedIndex == 1 then
        return
    end
    this.DoClickAdventure()
end
function UIBottom.DoClickAdventure(isSkip)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Adventure, true, bt_adventure) then
        lastSelectedIndex = 1;
        pageController.selectedIndex = lastSelectedIndex;
        if not logicMgr.ArenaManager.getSomeBattleSkip() then
            this.closeAllUI();
        end

        showUI(PanelNames.UIAdventureMain);
        if not isOpenUI(PanelNames.UIFuncMenu) then
            showUI(PanelNames.UIFuncMenu);
        else
            getUI(PanelNames.UIFuncMenu):GetController("c1").selectedIndex = 0;
        end
        if not isOpenUI(PanelNames.UIHeadMenu) then
            showUI(PanelNames.UIHeadMenu);
        end
        if not isOpenUI(PanelNames.BackGround) then
            showUI(PanelNames.BackGround);
        end
        UIHeadMenu.SetNoticeDown(false)
        FireEvent(Config.EventType.BottomChangePage, EnumConst.BottomPageType.adventure)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
end
function UIBottom.onClickBattle(isOn)
    this.UpdateCheck()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Battle, true, bt_battle) then
        pageController.selectedIndex = 2;
        lastSelectedIndex = 2;
        if FightManager.fightBegin then
            if isOpenUI(PanelNames.UIBattleMain) then
                return
            end
        else
            if isOpenUI(PanelNames.UIBattle) then
                return
            end
        end
        this.closeAllUI();
        if FightManager.fightBegin then
            hideOtherUI(false);
            local name = getUIName(PanelNames.UIBattleMain);
            panelMgr:ShowUI(name, UIBattleMain.OnCreate, Config.useLocalUI);
        else
            showUI(PanelNames.UIBattle);
            if not isOpenUI(PanelNames.UIFuncMenu) then
                showUI(PanelNames.UIFuncMenu, 1);
            else
                getUI(PanelNames.UIFuncMenu):GetController("c1").selectedIndex = 1;
            end
            if not isOpenUI(PanelResNames.UIHeadMenu) then
                showUI(PanelNames.UIHeadMenu);
            end
            UIHeadMenu.SetNoticeDown(true)
        end
        FireEvent(Config.EventType.BottomChangePage, EnumConst.BottomPageType.battle)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
    UISpeed()
end

function UIBottom.onClickPlayer(isOn)
    if lastSelectedIndex == 3 then
        return ;
    end
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Hero, true, bt_player) then
        lastSelectedIndex = 3;
        pageController.selectedIndex = 3
        this.closeAllUI();

        if not isOpenUI(PanelResNames.UIHeadMenu) then
            showUI(PanelNames.UIHeadMenu);
        end
        showUI(PanelNames.UICharactorMain);
        FireEvent(Config.EventType.BottomChangePage, EnumConst.BottomPageType.hero)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
end

function UIBottom.onClickHero(isOn)
    --showUI(PanelNames.UIDraw);
    if lastSelectedIndex == 4 then
        return ;
    end
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Draw, true, bt_hero) then
        lastSelectedIndex = 4;
        this.closeAllUI();
        showUI(PanelNames.UIDraw);
        if not isOpenUI(PanelNames.UIHeadMenu) then
            showUI(PanelNames.UIHeadMenu);
        end
        if not isOpenUI(PanelNames.BackGround) then
            showUI(PanelNames.BackGround);
        end
        UIHeadMenu.SetNoticeDown(false)
        FireEvent(Config.EventType.BottomChangePage, EnumConst.BottomPageType.draw)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
end

function UIBottom.onClickHeros(num)
    --showUI(PanelNames.UIDraw);
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Draw, true, bt_hero) then
        lastSelectedIndex = 4;
        this.closeSomeUI();
        showUI(PanelNames.UIDraw, num);
        if not isOpenUI(PanelNames.UIHeadMenu) then
            showUI(PanelNames.UIHeadMenu);
        end
        if not isOpenUI(PanelNames.BackGround) then
            showUI(PanelNames.BackGround);
        end
        UIHeadMenu.SetNoticeDown(false)
    else
        pageController.selectedIndex = lastSelectedIndex;
    end
end

function UIBottom.closeAllUI()
    --closeUI(PanelNames.UIHome);
    closeUI(PanelResNames.UICity);
    closeUI(PanelNames.UIAdventureMain);
    closeUI(PanelNames.UICharactorMain);
    closeUI(PanelNames.UIBattle);
    closeUI(PanelNames.UIBattleMain);
    closeUI(PanelNames.UIChatPanel);
    closeUI(PanelNames.UIDraw);
    closeUI(PanelNames.UIFuncMenu);
    closeUI(PanelNames.UIHeadMenu);
    closeUI(PanelNames.BackGround);

    --hideOtherUI(false);
end
function UIBottom.closeSomeUI()
    --closeUI(PanelNames.UIHome);
    closeUI(PanelResNames.UICity);
    closeUI(PanelNames.UIAdventureMain);
    closeUI(PanelNames.UIBattle);
    closeUI(PanelNames.UIBattleMain);
    closeUI(PanelNames.UIChatPanel);
    closeUI(PanelNames.UICharactorMain);
    closeUI(PanelNames.UIFuncMenu);

    --hideOtherUI(false);
end
--function UIBottom.onSetHome()
--    if bt_home.isOn then
--        this.onClickHome(true);
--    else
--        bt_home.isOn = true;
--    end
--end

function UIBottom.onSetTown()
    --if bt_town.isOn then
    lastSelectedIndex = -1
    this.onClickTown(true);
    pageController.selectedIndex = 0;

    --else
    --    bt_town.isOn = true;
    --end
end

function UIBottom.onSetBattle()
    --if bt_battle.isOn then
    pageController.selectedIndex = 2;
    this.onClickBattle(true);
    --else
    --    bt_battle.isOn = true;
    --end
end

function UIBottom.enterAdventure()
    pageController.selectedIndex = 1;
    this.DoClickAdventure();
end
function UIBottom.enterTown()
    pageController.selectedIndex = 0;
    this.DoClickTown();
end
function UIBottom.enterPlayer()
    pageController.selectedIndex = 3;
    this.onClickPlayer();
end
function UIBottom.enterDraw()
    pageController.selectedIndex = 4;
    this.onClickHero();
end
function UIBottom.enterDrawto(num)
    pageController.selectedIndex = 4;
    this.onClickHeros(num);
end
function UIBottom.UpdateCheck()
    if Time.realtimeSinceStartup - this.silenceCheckTime > 300 then
        this.silenceCheckTime = Time.realtimeSinceStartup
        print("CheckSilenceUpdate==")
        gameMgr:CheckSilenceUpdate()
    end
end

--[[

--背包红点显示



function UIBottom.bagEffect()
    if scaleEffectFinish == true then return end
    UISysTips.AddMsgById("0301_1")
    scaleEffectFinish = true
    local punch = bt_bag.transform:DOPunchScale(Vector3(0.2,0.2,0.2),0.3,0,1)
    punch:OnComplete(function()
        scaleEffectFinish = false
    end)
end

function UIBottom.RedPointChatActive(active)
    redPointChat:SetActive(active)
    UISocial.SetActiveChatBtnPoint(active)
end
function UIBottom.RedPointHomeActive(active)
    redPointHomeTown:SetActive(active)
end

---进副本换按钮，隐藏剧院
function UIBottom.SetDungeonsShow(active)

    bt_dungeons.gameObject:SetActive(active)
    bt_hometown.gameObject:SetActive(not active)
    btnPressImg["DungeonsBtn"]["upimg"]:SetActive(active);
    btnPressImg["DungeonsBtn"]["downimg"].gameObject:SetActive(false);

    btnPressImg["HometownBtn"]["upimg"]:SetActive(not active);
    btnPressImg["HometownBtn"]["downimg"].gameObject:SetActive(false);
    --if active then
        --this.OnBattleClick(bt_battle.gameObject);
    --end

    UIBattle.HideOpera(active)
end

--dungeons
function UIBottom.OnDungeonsClick(obj)
    --showUI(PanelNames.UIDungeonsInfo);
    local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
    pop.Init("71", function(b)
        if b then
            InstanceManager.Leave();
            this.BattleClick();
        end
    end)
end

function UIBottom.showHomeUI()
    this.OnHometownClick(bt_hometown.gameObject);
end

--引导打开对应界面，不受新手引导中不让打开的限制
function UIBottom.showHomeGuide(funid)

    this.SetBtnState(bt_hometown.gameObject);
    showUI(PanelNames.UIHome,funid);
end
--home
function UIBottom.OnHometownClick(obj)

    --if GuideModel.curStep < 0 or GuideModel.curStep >= 23 or (GuideModel.curGuideTable ~= nil and GuideModel.curGuideTable.hieraychy == "BTM/HometownBtn") then
        this.SetBtnState(obj);
        --this.OpenShopCtrl(false);
        showUI(PanelNames.UIHome);
    --else
    --    UISysTips.AddMsg("新手引导中，完成引导后开放");
   -- end
end


function UIBottom.BattleClick()
    this.OnBattleClick(bt_battle.gameObject);
    --showUI(PanelNames.UIBattle);
end



    showUI(PanelNames.UIDraw);
    this.SetBtnState(obj);
end

function UIBottom.OnHeroClickNoArg()
	this.OnHeroClick(bt_hero.gameObject);
end



--Email
function UIBottom.OnEmailClick(obj)
    if oldChoose == obj then
        return;
    end
    this.SetBtnState(obj);
    showUI(PanelNames.UIEmail);
end



--外部使用显示好友界面
function UIBottom.ShowFriendFrame()
    this.SetBtnState(bt_social.gameObject);
    --showUI(PanelNames.UISocial,"Friends");旧版本
    showUI(PanelNames.UIFriends);
end

--外部使用显示聊天界面
function UIBottom.ShowChatFrame(cb)
    this.SetBtnState(bt_social.gameObject);
    showUI(PanelNames.UISocial,"chat",this.ToGuildChatFrame);
end

function UIBottom.ToGuildChatFrame()
	local social = CtrlManager.GetCtrl(PanelNames.UISocial);
	if social ~= nil then
		social.ToGuildChatFrame();
	end

end

function UIBottom.ShowRepair(isRepair)
    repairPoint.gameObject:SetActive(isRepair)
end



--function UIBottom.OpenShopCtrl(isActive)
--
--    --uiShopObj:SetActive(isActive);
--    --bt_MojingShop.interactable = not isActive;
--    if isActive then
--        --this.OnReSetTapBtn();
--        --bt_MojingShop.interactable = false;
--        UIShop.OnClickTapBtn(2);
--    end
--    --bt_battle.gameObject:SetActive( not isActive);
--    --bt_player.gameObject:SetActive(not isActive);
--    --bt_hero.gameObject:SetActive(not isActive);
--    --bt_adventure.gameObject:SetActive(not isActive);
--end

function UIBottom.OnReSetTapBtn()
    bt_MojingShop.interactable = true;
    bt_GoldShop.interactable = true;
    bt_FriendShop.interactable = true;
    bt_UnitShop.interactable = true;
end

function UIBottom.OnSetActive(isAcive)
    gameObject:SetActive(isAcive);
end

]]

return UIBottom;